package model.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.dao.DBUtil;

public class SqlBase<T>{
	private Class<T> entity;
	private String tablename;
	private Field[] fd;
	
	@SuppressWarnings("unchecked")
	public SqlBase() {
		ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
	    entity=(Class<T>) type.getActualTypeArguments()[0];
	    tablename=entity.getSimpleName();
	    fd=entity.getDeclaredFields();
	}
	
	public List<T> getAllSelect(){
		List<T> list=new ArrayList<>();
		list.clear();
		StringBuffer sql=new StringBuffer("select * from " + tablename);
		try {
			Statement stm=DBUtil.getConn().createStatement();
			ResultSet rs=stm.executeQuery(sql.toString());
	
			while(rs.next()){
				T obj=entity.newInstance();
				  for(int i=0;i<fd.length;i++){
					  fd[i].setAccessible(true);
					  Object setval=rs.getObject(i+1);
					  fd[i].set(obj, setval);
				  }
				list.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Object> getSigleSelect(Object column,Object condCol,Object condVal){
		List<Object> list=new ArrayList<>();
	    list.clear();
		StringBuffer sql=new StringBuffer("select " + column + " from " + tablename + " where " + condCol + "=\'" + condVal + "\';"); 
		System.out.println(sql);
		try {
			Statement stm = DBUtil.getConn().createStatement();
			ResultSet rs=stm.executeQuery(sql.toString());
			
			while(rs.next()){
				list.add(rs.getObject((String)column));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public void insertData(T o){
		StringBuffer sql=new StringBuffer("insert into " + tablename + " (");
		for(int i=0;i<fd.length;i++){
			sql.append(fd[i].getName());
			if(i<fd.length-1){
				sql.append(",");
			}else{
				sql.append(") values(");
			}
		}
		
		for(int i=0;i<fd.length;i++){
			sql.append("?");
			if(i<fd.length-1){
				sql.append(",");
			}else{
				sql.append(")");
			}
		}
		
		System.out.println(sql);
		
		try {
			PreparedStatement pstm=DBUtil.getConn().prepareStatement(sql.toString());
			for(int i=0;i<fd.length;i++){
				fd[i].setAccessible(true);
				Object val=fd[i].get(o);
				pstm.setObject((i+1),val);
			}
			pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<T> getRanSelect(T o,String condit){
		List<T> list=new ArrayList<>();
		list.clear();
		StringBuffer sql=new StringBuffer("select * from " + tablename + " where ");
		for(int i=0;i<fd.length;i++){
			fd[i].setAccessible(true);
			try {
				if(fd[i].get(o)!=null){
					sql.append(fd[i].getName() + "='" + fd[i].get(o) + "' " + condit + " ");
				}
				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String str=sql.substring(0, sql.length()-4);
		sql.replace(0, sql.length(), str);
		System.out.println(sql.toString());
		
		try {
			Statement stm=DBUtil.getConn().createStatement();
			ResultSet rs=stm.executeQuery(sql.toString());
			
			while(rs.next()){
			    T obj=entity.newInstance();	
			    for(int i=0;i<fd.length;i++){
			    	Object val=rs.getObject(i+1);
			    	fd[i].set(obj, val);
			    }
			    list.add(obj);
			}
	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public void delete(T o,String condit){
		StringBuffer sql=new StringBuffer("delete from " + tablename + " where ");
		for(int i=0;i<fd.length;i++){
			fd[i].setAccessible(true);
			try {
				if(fd[i].get(o)!=null){
					sql.append(fd[i].getName() + "='" + fd[i].get(o) + "' " + condit + " ");
				}
				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String str=sql.substring(0, sql.length()-4);
		sql.replace(0, sql.length(), str);
		System.out.println(sql.toString());
		
		try {
			Statement stm=DBUtil.getConn().createStatement();
			stm.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void update(T o,T o1,String condit){
		StringBuffer sql=new StringBuffer("update " + tablename + " set ");
		for(int i=0;i<fd.length;i++){
			fd[i].setAccessible(true);
			try {
				if(fd[i].get(o)!=null){
					if(fd[i].getType().getSimpleName().indexOf("String")!=-1){
					    sql.append(fd[i].getName() + "=\"" + fd[i].get(o) + "\",");
					}
					if(fd[i].getType().getSimpleName().indexOf("Integer")!=-1){
						sql.append(fd[i].getName() + "=" + fd[i].get(o) + ",");
					}
				}
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String str=sql.toString();
		str=str.substring(0, str.length()-1);
		sql=sql.replace(0, sql.length(), str);
		sql.append(" where ");
		
		for(int i=0;i<fd.length;i++){
			fd[i].setAccessible(true);
			try {
				if(fd[i].get(o1)!=null){
					if(fd[i].getType().getSimpleName().indexOf("String")!=-1){
					    sql.append(fd[i].getName() + "=\"" + fd[i].get(o1) + "\" " + condit + " ");
					}
					if(fd[i].getType().getSimpleName().indexOf("Integer")!=-1){
						sql.append(fd[i].getName() + "=" + fd[i].get(o1) + " " + condit + " ");
					}
				}
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String str1=sql.toString();
		str1=str1.substring(0, str1.length()-4);
		sql.replace(0, sql.length(), str1);
	    
	    System.out.println(sql.toString());
		
	    try {
			Statement stm=DBUtil.getConn().createStatement();
			stm.executeUpdate(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}










































































